/**
 * Created by chenyong on 2015/7/16.
 */

var passport = require('passport');
var request = require('request');
var WechatStrategy = require('./passport-wechat').Strategy;

exports.setup = function (config){
    passport.use(new WechatStrategy({
            clientID: config.wechat.clientID,
            clientSecret: config.wechat.clientSecret,
            callbackURL: config.wechat.callbackURL
        },
        function(accessToken, refreshToken, profile, done) {

            user = {
                role:'user',
                wechat:JSON.stringify(profile)
            };

            var req_panoinfo = {
                url: config.api.rootpath + '/api/users',
                json:true,
                headers: {'Authorization': 'Bearer ' + config.api.token()},
                form:user
            };

            request.post(req_panoinfo, function(error, response, data){
                return done(error, data);
                //return res.json(201, JSON.parse(data));
            });

            //将用户信息写入数据库
            //return done(null, profile);
        }
    ));

};